package com.gitee.zycra.union.api.manager;

import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.model.ApiInterface;
import com.gitee.zycra.union.api.common.param.AddInterfaceParam;
import com.gitee.zycra.union.api.common.param.PageQueryInterfaceParam;
import com.gitee.zycra.union.api.common.param.UpdateInterfaceParam;
import com.gitee.zycra.union.api.common.util.IdGenerator;
import com.gitee.zycra.union.api.common.vo.InterfaceVO;
import com.gitee.zycra.union.api.dao.ApiInterfaceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zycra
 */
@Component
public class ApiInterfaceManager {

    @Autowired
    private ApiInterfaceDao apiInterfaceDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 根据目录ID查询接口数量
     *
     * @param folderId 目录ID
     * @return 接口数量
     */
    public int countByFolderId(Long folderId) {
        return apiInterfaceDao.countByFolderId(folderId);
    }

    /**
     * 添加接口
     *
     * @param addInterfaceParam 添加参数
     * @return 影响行数
     */
    public int addInterface(AddInterfaceParam addInterfaceParam) {
        ApiInterface apiInterface = AddInterfaceParam.convertToApiInterface(addInterfaceParam);
        apiInterface.setId(idGenerator.nextId());
        return apiInterfaceDao.insert(apiInterface);
    }

    /**
     * 更新接口
     *
     * @param updateInterfaceParam 更新参数
     * @return 影响行数
     */
    public int updateInterface(UpdateInterfaceParam updateInterfaceParam) {
        ApiInterface apiInterface = UpdateInterfaceParam.convertToApiInterface(updateInterfaceParam);
        return apiInterfaceDao.updateById(apiInterface);
    }

    /**
     * 删除接口
     *
     * @param id 接口ID
     * @return 影响行数
     */
    public int deleteInterface(Long id) {
        return apiInterfaceDao.updateToDelete(id);
    }

    /**
     * 查询接口
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    public PageResult<InterfaceVO> pageQueryInterface(PageQuery<PageQueryInterfaceParam> pageQuery) {
        return apiInterfaceDao.pageQueryInterface(pageQuery);
    }
}
